/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sessionBean;

import entity.Book;
import entity.Shelf;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.ejb.LocalBean;
import sbfec.BookFacade;
import sbfec.ShelfFacade;

/**
 *
 * @author Admin
 */
@Stateless
@LocalBean
public class GestoreLibri {
    @EJB
    private ShelfFacade shelfFacade;
    @EJB
    private BookFacade bookFacade;
    
    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")

    
    public List<Shelf> getShelves() {
        return shelfFacade.findAll();
    }

    public List<Book> getBooksInShelf(Shelf shelf) {
        return shelf.getBooks();
    }
    
    public List<Book> getAllBooks(){
        return bookFacade.findAll();
    }
    
    public void addBook(String title, String author, String shelf) {
        Book b = new Book();
        b.setName(title);
        b.setAuthor(author);
        b.setShelf(getShelfFromGenre(shelf));
        bookFacade.create(b);
    }

    private Shelf getShelfFromGenre(String shelf) {
        List<Shelf> shelves = shelfFacade.findAll();
        for (Shelf s: shelves){
            if (s.getGenre().equals(shelf)) return s;
        }
        Shelf s = new Shelf();
        s.setGenre(shelf);
        shelfFacade.create(s);
        return s;
    }
    
    public int numberOfBooks(){
        return bookFacade.count();
    }
    
}
